package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: ggm
 * @data 2024年12月10日 20:42
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //只能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //先判断购物车里是否有这个商品即查询数据库
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if(shoppingCartList!=null&&shoppingCartList.size()>0){
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            //如果有则更新商品的数量
            shoppingCartMapper.updateNumberById(shoppingCart);
            return;
        }
        //没有则先判断要添加到购物车的是菜品还是套餐
        Long dishId = shoppingCartDTO.getDishId();
        if(dishId!=null){
            //若为菜品则添加菜品
            Dish dish = dishMapper.getById(dishId);
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
            //shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        } else {
            //添加到购物车的商品为套餐
            Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        //为套餐或者菜品添加公共的属性 数量都为1
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //添加到数据库
        shoppingCartMapper.add(shoppingCart);
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        //根据登陆时存储的userid查询个人用户的购物车
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(currentId);
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void clean() {
        //根据userid清空购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(currentId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //先判断购物车里是否有这个商品即查询数据库
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //遍历购物车集合，使购物车中的商品数量减1
        for (ShoppingCart cart : shoppingCartList) {
            //如果商品的数量为1则从购物车中删除商品
            if(cart.getNumber()==1){
                //判断要删除的商品是菜品还是套餐
                if(shoppingCartDTO.getDishId()!=null){
                    shoppingCartMapper.deleteByDishId(shoppingCartDTO.getDishId());
                }else {
                    shoppingCartMapper.deleteBySetmealId(shoppingCartDTO.getSetmealId());
                }
            }else {
                cart.setNumber(cart.getNumber()-1);
                //更新购物车中商品数量
                shoppingCartMapper.updateNumberById(cart);
            }
        }
    }
}
